<template>
  <div class="orrOutcupEfficiencyDetail-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="门店组" prop="storeName">
          <el-input
            placeholder="请输入门店组"
            v-model="searchForm.storeName"
            autocomplete="off"
          />
        </el-form-item>
        <!-- <el-form-item label="门店名称" prop="storeName">
          <el-input
            placeholder="请输入门店名称"
            v-model="searchForm.storeName"
            autocomplete="off"
          />
        </el-form-item> -->
        <el-form-item label="选择门店" prop="storeNumber">
          <el-input
            placeholder="请输入门店编号"
            v-model="searchForm.storeNumber"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item label="选择配方" prop="productNumber">
          <el-input
            placeholder="请输入选择配方"
            v-model="searchForm.productNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="规格项" prop="menuName">
          <el-input
            placeholder="请输入规格项"
            v-model="searchForm.menuName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="选择日期" prop="menuName">
          <el-input
            placeholder="请输入选择日期"
            v-model="searchForm.menuName"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
            <el-button type="success" @click="startClear"> 导出报表 </el-button>
            <el-button type="success" @click="startClear">
              导出明细报表
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="storeName" label="门店名称" width="140" />
          <el-table-column prop="date" label="日期" width="140" />
          <el-table-column prop="productNumber" label="配方名称" width="190" />
          <el-table-column prop="menuName" label="规格项" width="190" />
          <el-table-column
            prop="estimateValue"
            label="配方出料量(g)"
            width="130"
          />
          <el-table-column
            prop="materialValue"
            label="实际出料量(g)"
            width="130"
          />
          <el-table-column
            prop="clientStartTime"
            label="制作开始时间"
            width="130"
          />
          <el-table-column
            prop="clientFinishTime"
            label="制作完成时间"
            width="130"
          />
          <el-table-column
            prop="cupOutSpeed"
            label="出杯速度（杯/秒）"
            width="150"
          />

          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <OrrOutcupEfficiencyDetailAdd
      v-model="addUpdateRef"
    ></OrrOutcupEfficiencyDetailAdd>
    <OrrOutcupEfficiencyDetailRemove
      v-model="removeRef"
    ></OrrOutcupEfficiencyDetailRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getOrrOutcupEfficiencyDetailPagination,
  getOrrOutcupEfficiencyDetailAllList,
} from '@/api/orr-outcup-efficiency-detail.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import OrrOutcupEfficiencyDetailRemove from './OrrOutcupEfficiencyDetailRemove.vue'
import OrrOutcupEfficiencyDetailAdd from './OrrOutcupEfficiencyDetailAdd.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'

const titleName = `出杯效率明细报表表`
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `出杯效率明细报表表`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

// const requestCanUseCode = () => {
// 	getCanUseCode({ type: 8 })
// 			.then((res: any) => {
// 				console.log('getCanUseCode: ', res)
// 				addUpdateRef.form.number = res
// 			})
// 			.catch((err) => {
// 				ElMessage.error(err.msg)
// 			})
// }

const showAddEditDialog = (item: any, type: number) => {
  //新增
  if (item.target) {
    let form = {
      id: null,
      uuid: '',
      storeName: '',
      clientCreateTime: '',
      clientFinishTime: '',
      clientStartTime: '',
      productNumber: '',
      menuName: '',
      cupOutSpeed: '',
      materialValue: '',
      estimateValue: '',
      materialValueStr: '',
      estimateValueStr: '',
      missValue: '',
      missValueDouble: '',
      storeNumber: '',
      cupOutSpeedString: '',
      quantity: '',
      specName: '',
      machineUuid: '',
      pouringTime: '',
      pouringTimeStr: '',
      operateTime: '',
      operateTimeStr: '',
      disabled: false,
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    // requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    uuid: item.uuid,
    storeName: item.storeName,
    clientCreateTime: item.clientCreateTime,
    clientFinishTime: item.clientFinishTime,
    clientStartTime: item.clientStartTime,
    productNumber: item.productNumber,
    menuName: item.menuName,
    cupOutSpeed: item.cupOutSpeed,
    materialValue: item.materialValue,
    estimateValue: item.estimateValue,
    materialValueStr: item.materialValueStr,
    estimateValueStr: item.estimateValueStr,
    missValue: item.missValue,
    missValueDouble: item.missValueDouble,
    storeNumber: item.storeNumber,
    cupOutSpeedString: item.cupOutSpeedString,
    quantity: item.quantity,
    specName: item.specName,
    machineUuid: item.machineUuid,
    pouringTime: item.pouringTime,
    pouringTimeStr: item.pouringTimeStr,
    operateTime: item.operateTime,
    operateTimeStr: item.operateTimeStr,
    disabled: false,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: `出杯效率明细报表表`,
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  uuid: '',
  storeName: '',
  clientCreateTime: '',
  clientFinishTime: '',
  clientStartTime: '',
  productNumber: '',
  menuName: '',
  cupOutSpeed: '',
  materialValue: '',
  estimateValue: '',
  materialValueStr: '',
  estimateValueStr: '',
  missValue: '',
  missValueDouble: '',
  storeNumber: '',
  cupOutSpeedString: '',
  quantity: '',
  specName: '',
  machineUuid: '',
  pouringTime: '',
  pouringTimeStr: '',
  operateTime: '',
  operateTimeStr: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let uuid = searchForm.value.uuid
  if (uuid) {
    searchCondition.uuid = uuid
  }
  let storeName = searchForm.value.storeName
  if (storeName) {
    searchCondition.storeName = storeName
  }
  let clientCreateTime = searchForm.value.clientCreateTime
  if (clientCreateTime) {
    searchCondition.clientCreateTime = clientCreateTime
  }
  let clientFinishTime = searchForm.value.clientFinishTime
  if (clientFinishTime) {
    searchCondition.clientFinishTime = clientFinishTime
  }
  let clientStartTime = searchForm.value.clientStartTime
  if (clientStartTime) {
    searchCondition.clientStartTime = clientStartTime
  }
  let productNumber = searchForm.value.productNumber
  if (productNumber) {
    searchCondition.productNumber = productNumber
  }
  let menuName = searchForm.value.menuName
  if (menuName) {
    searchCondition.menuName = menuName
  }
  let cupOutSpeed = searchForm.value.cupOutSpeed
  if (cupOutSpeed) {
    searchCondition.cupOutSpeed = cupOutSpeed
  }
  let materialValue = searchForm.value.materialValue
  if (materialValue) {
    searchCondition.materialValue = materialValue
  }
  let estimateValue = searchForm.value.estimateValue
  if (estimateValue) {
    searchCondition.estimateValue = estimateValue
  }
  let materialValueStr = searchForm.value.materialValueStr
  if (materialValueStr) {
    searchCondition.materialValueStr = materialValueStr
  }
  let estimateValueStr = searchForm.value.estimateValueStr
  if (estimateValueStr) {
    searchCondition.estimateValueStr = estimateValueStr
  }
  let missValue = searchForm.value.missValue
  if (missValue) {
    searchCondition.missValue = missValue
  }
  let missValueDouble = searchForm.value.missValueDouble
  if (missValueDouble) {
    searchCondition.missValueDouble = missValueDouble
  }
  let storeNumber = searchForm.value.storeNumber
  if (storeNumber) {
    searchCondition.storeNumber = storeNumber
  }
  let cupOutSpeedString = searchForm.value.cupOutSpeedString
  if (cupOutSpeedString) {
    searchCondition.cupOutSpeedString = cupOutSpeedString
  }
  let quantity = searchForm.value.quantity
  if (quantity) {
    searchCondition.quantity = quantity
  }
  let specName = searchForm.value.specName
  if (specName) {
    searchCondition.specName = specName
  }
  let machineUuid = searchForm.value.machineUuid
  if (machineUuid) {
    searchCondition.machineUuid = machineUuid
  }
  let pouringTime = searchForm.value.pouringTime
  if (pouringTime) {
    searchCondition.pouringTime = pouringTime
  }
  let pouringTimeStr = searchForm.value.pouringTimeStr
  if (pouringTimeStr) {
    searchCondition.pouringTimeStr = pouringTimeStr
  }
  let operateTime = searchForm.value.operateTime
  if (operateTime) {
    searchCondition.operateTime = operateTime
  }
  let operateTimeStr = searchForm.value.operateTimeStr
  if (operateTimeStr) {
    searchCondition.operateTimeStr = operateTimeStr
  }

  tableLoading.value = true
  getOrrOutcupEfficiencyDetailPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.orrOutcupEfficiencyDetail-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.orrOutcupEfficiencyDetail-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
